﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace DIS.Infrastructure.Entities.Repository
{
    [Table("T_LongHaulDetail")]
    public class T_LongHaulDetail : IEntity<T_LongHaulDetail>
    {
        [Key]
        [Column(TypeName = "int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int LongHaulId { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string LongHaulDescription { get; set; }

        public int Days { get; set; }
         
        public decimal TotalFee { get; set; }

        public int InvoiceCount { get; set; }

        public decimal AllowanceMoney { get; set; }

        public override T_LongHaulDetail Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_LongHaulDetail Find(System.Linq.Expressions.Expression<Func<T_LongHaulDetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.LongHaulDetail.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.LongHaulId = item.LongHaulId;
                    this.LongHaulDescription = item.LongHaulDescription;
                    this.Days = item.Days;
                    this.TotalFee = item.TotalFee;
                    this.InvoiceCount = item.InvoiceCount;
                    this.AllowanceMoney = item.AllowanceMoney;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_LongHaulDetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.LongHaulDetail.Any(where);
            }
        }

        public override T_LongHaulDetail FindLast(System.Linq.Expressions.Expression<Func<T_LongHaulDetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.LongHaulDetail.OrderByDescending(c => c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.LongHaulId = item.LongHaulId;
                    this.LongHaulDescription = item.LongHaulDescription;
                    this.Days = item.Days;
                    this.TotalFee = item.TotalFee;
                    this.InvoiceCount = item.InvoiceCount;
                    this.AllowanceMoney = item.AllowanceMoney;
                    return this;
                }
                return null;
            }
        }
    }
}
